package org.jeecg.modules.chargeElectricity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.chargeElectricity.entity.ChargeElectricity;
import org.jeecg.modules.chargeElectricity.entity.ChargeElectricityDetail;
import org.jeecg.modules.chargeElectricity.mapper.ChargeElectricityDetailMapper;
import org.jeecg.modules.chargeElectricity.mapper.ChargeElectricityMapper;
import org.jeecg.modules.chargeElectricity.service.IChargeElectricityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 电价方案
 * @Author: jeecg-boot
 * @Date:   2023-08-31
 * @Version: V1.0
 */
@Service
public class ChargeElectricityServiceImpl extends ServiceImpl<ChargeElectricityMapper, ChargeElectricity> implements IChargeElectricityService {

	@Autowired
	private ChargeElectricityMapper chargeElectricityMapper;
	@Autowired
	private ChargeElectricityDetailMapper chargeElectricityDetailMapper;

	@Override
	public void updateStatus(ChargeElectricity chargeElectricity){
		if ("默认方案".equals(chargeElectricity.getStatus())){
			ChargeElectricity electricity = new ChargeElectricity();
			electricity.setStatus("备用方案");
			chargeElectricityMapper.update(electricity,null);
		}
	}

	@Override
	public List<ChargeElectricityDetail> getProgramme() {
		LambdaQueryWrapper<ChargeElectricity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(ChargeElectricity::getStatus, "默认方案");
		ChargeElectricity chargeElectricity = chargeElectricityMapper.selectOne(queryWrapper);
		String id = chargeElectricity.getId();
		return chargeElectricityDetailMapper.selectByMainId(id);
	}
	
	@Override
	@Transactional
	public void saveMain(ChargeElectricity chargeElectricity, List<ChargeElectricityDetail> chargeElectricityDetailList) {
		chargeElectricityMapper.insert(chargeElectricity);
		if(chargeElectricityDetailList!=null && chargeElectricityDetailList.size()>0) {
			for(ChargeElectricityDetail entity:chargeElectricityDetailList) {
				//外键设置
				entity.setChargeElectricityId(chargeElectricity.getId());
				chargeElectricityDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ChargeElectricity chargeElectricity,List<ChargeElectricityDetail> chargeElectricityDetailList) {
		chargeElectricityMapper.updateById(chargeElectricity);
		
		//1.先删除子表数据
		chargeElectricityDetailMapper.deleteByMainId(chargeElectricity.getId());
		
		//2.子表数据重新插入
		if(chargeElectricityDetailList!=null && chargeElectricityDetailList.size()>0) {
			for(ChargeElectricityDetail entity:chargeElectricityDetailList) {
				//外键设置
				entity.setChargeElectricityId(chargeElectricity.getId());
				chargeElectricityDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		chargeElectricityDetailMapper.deleteByMainId(id);
		chargeElectricityMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chargeElectricityDetailMapper.deleteByMainId(id.toString());
			chargeElectricityMapper.deleteById(id);
		}
	}
	
}
